/* ************************************************************************

   Copyright:
     2008 openWorks

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Juan Matias Granda

************************************************************************ */

/**
* Permite enviar datos de forma simple a uno o varios mails.
* 
* *Ejemplo:*
*
* <pre class='javascript'>
* var datos = [
*  {to: 'john&#64;doe.com'},
*  {to: 'johnny&#64;doe.com'},
*  {from: 'qx.ow&#64;openworks.com.ar'},
*  {replyTo: 'qx.ow&#64;openworks.com.ar'},
*  {subject:'Titulo del email'},
*  { data: '* dato 1:  Cuerpo del Body'},
*  { data: '* dato 2: ' + txtdato2.getValue() },
*  { data: '* dato 3: Ultimo dato del Body '}
* ]
* qx.ui.form.ow.SendMail.send(datos);
* 
*  </pre>
*/
qx.Class.define("qx.ui.form.ow.SendMail",{
	extend : qx.core.Object,
	
/*
*****************************************************************************
     STATICS
*****************************************************************************
*/
	statics :{
		/**
		* Metodo que dispara el envio del mail.
		* @param  structure {Map} Array de datos. <br />
		* *Parametros:*
		* <ul>
		* <li>*to:* mail de destino (pueden existir varios _to_).</li>
		* <li>*from:* mail que envia.</li>
		* <li>*replyTo:* mail al que se debe responder (si no se agrega _replyTo_ pasa a valer lo mismo que _from_).</li>
		* <li>*subject:* Titulo del mail.</li>
		* <li>*data:* Contenido que se escribira en el cuerpo del mail (pueden existir varios _data_).</li>
		* </ul>
		* @return {void}
		*/
		send: function (structure){
			var r ="";
			var mailTo = "";
			var body = "";
			for(r in structure){
				if(structure[r].to) {
					mailTo = mailTo + structure[r].to + ",";
				}
				if(structure[r].from){
					var from = structure[r].from;
				}
				if(structure[r].replyTo){
					var replyTo = structure[r].replyTo;
				}
				if(structure[r].subject){
					var subject = structure[r].subject;
				}
				if(structure[r].data) {
					body = body + structure[r].data + "<br />";
				}
			}
			if (!(replyTo)){
				replyTo = from;
			}
			var ajax = new qx.io.remote.Request("xml.php", "GET", "application/xml");
				ajax.setParameter("routine", "sendMail");
				ajax.setParameter("mailTo", mailTo);
				ajax.setParameter("from", from);
				ajax.setParameter("replyTo", replyTo);
				ajax.setParameter("subject", subject);
				ajax.setParameter("body", body);
				ajax.setAsynchronous(false);
				ajax.send();
		}
	}
});